package org.hhchat.leetcode.array;

public class code79_medium {

    static class Solution {
        public boolean exist(char[][] board, String word) {
            char[] chars = word.toCharArray();
            for(int i = 0;i < board.length; i++) {
                for(int j = 0 ; j < board[i].length; i++) {
                    if(search(board, chars, i , j ,0)){
                        return true;
                    }
                }
            }
            return false;
        }

        public boolean search(char[][] board, char[] word, int y , int x, int start) {
            if (start == word.length) {
                return true;
            }
            if (y < 0 || x < 0 || x == board[0].length || y == board.length) {
                return false;
            }
            if(board[y][x]!=word[start]) return false;
            board[y][x] ^= 256;
            boolean exist = search(board, word, y + 1, x, start + 1)
                    || search(board, word, y - 1, x, start + 1)
                    || search(board, word, y, x + 1, start + 1)
                    || search(board, word, y, x - 1, start + 1);
            board[y][x] ^= 256;
            return exist;

        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        char a = 'c';
        a ^= 256;
        System.out.println(a);
    }
}
